#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Author    :Timo

# 登录认证类LoginRequiredMixin
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import ListView, DeleteView
from django.template.loader import render_to_string
from django.http import HttpResponse, HttpResponseBadRequest, JsonResponse
from django.contrib.auth.decorators import login_required
from django.views.decorators.http import require_http_methods
from django.urls import reverse_lazy

from zanhu.news.models import News
from zanhu.utils.utils import ajax_required, AuthorRequireMixin


class NewsListView(LoginRequiredMixin, ListView):
    """
    首页动态
    """
    # 指定关联的模型类
    model = News
    # 分页 url中的?page=
    paginate_by = 20
    # 指定前端文件 默认为模型类名_list.html
    template_name = "news/news_list.html"
    context_object_name = "news_list"

    def get_queryset(self):
        return News.objects.filter(reply=False)


class NewsDeleteView(LoginRequiredMixin, AuthorRequireMixin, DeleteView):
    """
    首页动态
    """
    # 指定关联的模型类
    model = News
    # 指定前端文件 默认为模型类名_list.html
    template_name = "news/news_confirm_delete.html"
    # 通过url传入要删除的主键id, 默认值slug
    # slug_url_kwarg = 'slug'
    # 通过url传入要删除的主键id, 默认值pk
    pk_url_kwarg = 'pk'
    # 成功后跳转的路径
    # reverse_lazy在项目URLConf未加载前使用
    success_url = reverse_lazy("news:list")

    def get_queryset(self):
        return News.objects.filter(reply=False)


@login_required
@ajax_required
@require_http_methods(["POST"])
def post_news(request):
    """
    发送动态, AJAX POST请求
    :param request:
    :return:
    """
    post = request.POST.get('post')
    if post:
        post = post.strip()
        posted = News.objects.create(user=request.user, content=post)
        html = render_to_string('news/news_single.html', {'news': posted, 'request': request})
        return HttpResponse(html)
    else:
        return HttpResponseBadRequest("内容不能为空")


@login_required
@ajax_required
@require_http_methods(["POST"])
def liked(request):
    """
    点赞, AJAX POST请求
    :param request:
    :return:
    """
    news_id = request.POST.get('news')
    news = News.objects.get(pk=news_id)
    # 取消或添加赞
    news.switch_like(request.user)
    return JsonResponse({"likes": news.likers_count()})


@login_required
@ajax_required
@require_http_methods(["GET"])
def get_comment(request):
    """
    获取动态的评论, AJAX GET请求
    :param request:
    :return:
    """
    news_id = request.GET.get('news')
    news = News.objects.get(pk=news_id)
    # 没有评论时
    news_html = render_to_string('news/news_single.html', {'news': news})
    # 有评论时
    parent_html = render_to_string('news/news_comment.html', {'parent': news.get_child()})
    return JsonResponse({
        "uuid": news_id,
        "news": news_html,
        "parent": parent_html
    })


@login_required
@ajax_required
@require_http_methods(["POST"])
def post_comment(request):
    """
    评论, AJAX POST请求
    :param request:
    :return:
    """
    reply = request.POST.get('reply')
    parent_id = request.POST.get('parent')
    parent = News.objects.get(pk=parent_id)
    if reply:
        reply = reply.strip()
        parent.reply_this(request.user, reply)
        return JsonResponse({"comments": parent.comment_count()})
    else:
        return HttpResponseBadRequest("内容不能为空")
